package com.taxshare.tools.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.nio.charset.Charset;
import java.util.Map;

public class HttpClientUtil {

    /**
     * 连接超时:5s
     */
    private static final int connectTimeout=5000;

    /**
     * 响应超时:60s
     */
    private static final int readTimeout=60000;

    public static String sendGetHead(String url,Map<String,Object> paramMap){
        return HttpRequest.get(url)
                .header("accept","*/*")
                .header("connection","Keep-Alive")
                .header("TENANT-ID","11")
                .header("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)")
                .charset("UTF-8")
                .setConnectionTimeout(connectTimeout)
                .setReadTimeout(readTimeout)
                .form(paramMap)
                .execute().body();
    }

    public static String sendGet(String url,Map<String,Object> paramMap){
       return HttpRequest.get(url)
                .header("accept","*/*")
                .header("connection","Keep-Alive")
                .header("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)")
                .charset("UTF-8")
                .setConnectionTimeout(connectTimeout)
                .setReadTimeout(readTimeout)
                .form(paramMap)
                .execute().body();
    }

    public static String sendPostForm(String url,Map<String,Object> paramMap){
        return HttpRequest.post(url)
                .header("Content-type","application/x-www-form-urlencoded")
                .header("user-agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
                .charset("UTF-8")
                .setConnectionTimeout(connectTimeout)
                .setReadTimeout(readTimeout)
                .form(paramMap)
                .execute().body();
    }

    public static String sendPostJSON(String url,Map<String,Object> paramMap){
        return HttpRequest.post(url)
                .header("Content-type","application/json")
                .header("user-agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
                .charset("UTF-8")
                .setConnectionTimeout(connectTimeout)
                .setReadTimeout(readTimeout)
                .body(JSONUtil.toJsonStr(paramMap))
                .execute().body();
    }


}
